Shader "post/transDoor3"
{
	Properties
	{
		_FrostTex ("Fross Texture", 2D) = "white" {}
        _LightTex ("LightTex", 2D) = "black" {}
        _EdgeTex ("EdgeTex", 2D) = "black" {}
        _InterSecColor("InterSecColor",Color) = (1,1,1,1)
		_FrostIntensity ("Frost Intensity", Range(0.0, 1.0)) = 0.5
        _IntersectScale ("IntersectScale", Range(0,10)) = 10 //Range(50,0.1
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" "Queue" = "Transparent" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			#include "UnityCG.cginc"

			struct v2f
			{
                half2 uv : TEXCOORD0;
				float2 uvfrost : TEXCOORD1;
				float4 scrPos : TEXCOORD2;  
				float4 vertex : SV_POSITION;
			};

			sampler2D _FrostTex;
			float4 _FrostTex_ST;
            fixed4 _InterSecColor;

			float _FrostIntensity;
            float _IntersectScale;

			sampler2D _GrabBlurTexture_0;
			sampler2D _GrabBlurTexture_1;
			sampler2D _GrabBlurTexture_2;
			sampler2D _GrabBlurTexture_3;
            sampler2D _LightTex;
            sampler2D _EdgeTex;

            sampler2D _DepthTexture;

			v2f vert (appdata_full v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.texcoord;
				o.uvfrost = TRANSFORM_TEX(v.texcoord, _FrostTex);
				o.scrPos = ComputeGrabScreenPos(o.vertex);

				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				float surfSmooth = 1-tex2D(_FrostTex, i.uvfrost) * _FrostIntensity;
				
				surfSmooth = clamp(0, 1, surfSmooth);

                //float4 depth = tex2D(_DepthTexture, i.scrPos);
                //float4 depth = tex2D(_CameraDepthTexture, i.scrPos);
                //float depth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_DepthTexture, UNITY_PROJ_COORD(i.scrPos))); 

                //--------- 相交处 ------------     
                //利用屏幕坐标采样深度图，并对深度解码得到深度图中的线性深度（摄像机空间）
                float depthTexDepth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_DepthTexture, UNITY_PROJ_COORD(i.scrPos))); 
                float screenDepth = i.scrPos.z / i.scrPos.w; //经过齐次除法，得到屏幕深度值
                screenDepth = (UNITY_NEAR_CLIP_VALUE >= 0) ? screenDepth : screenDepth * 0.5 + 0.5; //根据平台不同，修正屏幕深度值
                screenDepth = LinearEyeDepth(screenDepth); //得到线性的屏幕深度
                //得到深度差值，并用参数控制大小
                float distanceRele = 1.0f - clamp((depthTexDepth - screenDepth) * _IntersectScale, 0, 1) ; 
                //distanceRele = abs(depthTexDepth)/25; 

                return float4(depthTexDepth/25 * float3(1,1,1),1 );
                fixed4 intersectColor =  _InterSecColor;

                //--------- 模糊 ------------
				half4 refraction;

				half4 ref00 = tex2Dproj(_GrabBlurTexture_0, i.scrPos);
				half4 ref01 = tex2Dproj(_GrabBlurTexture_1, i.scrPos);
				half4 ref02 = tex2Dproj(_GrabBlurTexture_2, i.scrPos);
				half4 ref03 = tex2Dproj(_GrabBlurTexture_3, i.scrPos);

				float step00 = smoothstep(0.75, 1.00, surfSmooth);
				float step01 = smoothstep(0.5, 0.75, surfSmooth);
				float step02 = smoothstep(0.05, 0.5, surfSmooth);
				float step03 = smoothstep(0.00, 0.05, surfSmooth);

				refraction = lerp(ref03, lerp( lerp( lerp(ref03, ref02, step02), ref01, step01), ref00, step00), step03);

                //--------- 边框 ------------
                float edge1 = 1 - tex2D(_EdgeTex, i.uv).x;
                float phase0 = frac(_Time.y + 0.5) ;
                float phase1 = frac(_Time.y) ;

                float4 color0 = tex2D(_LightTex, i.uv + phase0 ) ;
                float4 color1 = tex2D(_LightTex, i.uv + phase1 );

                float flowLerp = (abs(phase0 - 0.5)) * 2;
                float4 lightColor = lerp(color0, color1, flowLerp);

				
                //return refraction;
			    return lerp(refraction, intersectColor, distanceRele) + (edge1) * lightColor;

			}
			ENDCG
		}
	}
    FallBack Off
}
